package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"server/internal/model"
	"strconv"
)

func GetProject(c *gin.Context) {
	project := &model.Project{}
	projects, total := project.List()
	c.JSON(http.StatusOK, gin.H{
		"total": total,
		"data":  projects})
}

func GetProjectUsers(c *gin.Context) {
	id := c.Param("id")
	project := &model.Project{}
	project.ID, _ = strconv.Atoi(id)
	users := project.ListUser()
	c.JSON(http.StatusOK, gin.H{
		"data": users})
}

func CreateProject(c *gin.Context) {
	_ = c.Request.ParseForm()
	project := &model.Project{}
	_ = c.ShouldBind(project)
	err := project.Create()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": err.Error()})
	} else {
		c.String(http.StatusOK, "项目创建成功")
	}
}

func AddUserToProject(c *gin.Context) {
	_ = c.Request.ParseForm()
	project := &model.Project{}
	_ = c.ShouldBind(project)

	err := project.AddUser()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": err.Error()})
	} else {
		c.String(http.StatusOK, "添加成员成功")
	}
}

func RemoveUserToProject(c *gin.Context) {
	_ = c.Request.ParseForm()
	project := &model.Project{}
	_ = c.ShouldBind(project)

	err := project.RemoveUser()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": err.Error()})
	} else {
		c.String(http.StatusOK, "移除成员成功")
	}
}

func ModifyProject(c *gin.Context) {

}

func DeleteProject(c *gin.Context) {
	id := c.Param("id")
	peroject := &model.Project{}
	peroject.ID, _ = strconv.Atoi(id)
	err := peroject.Delete()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": "删除项目失败",
		})
		return
	}
	c.String(http.StatusOK, "删除项目成功")
}
